package com.my.mall.manage.user.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.mall.common.R;
import com.my.mall.manage.user.service.UserManageService;
import com.my.mall.manage.user.vo.MangerInfoVo;
import com.my.mall.manage.user.vo.UserInfoVo;
import com.my.mall.util.SecretUtil;

@Controller
@RequestMapping("manage/user")
public class UserManageController {
	@Autowired
	UserManageService userManageService;

	@RequestMapping("selectUserList")
	@ResponseBody
	public Map<String, Object> selectUserList(int iDisplayStart, int iDisplayLength) {
		List<UserInfoVo> list = userManageService.selectUserList(iDisplayStart, iDisplayLength);
		int size = userManageService.selectUserCount().size();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("iTotalRecords", size);
		map.put("iTotalDisplayRecords", size);
		map.put("aaData", list);
		return map;
	}

	@RequestMapping("save")
	@ResponseBody
	public R save(int id, int state) {
		int result = 0;
		result = userManageService.update(id, state);
		if (result == 1)
			return R.ok();
		else
			return R.error("系统故障！");
	}

	@RequestMapping("getUser/{id}")
	@ResponseBody
	public R getUser(@PathVariable("id") int id) {
		UserInfoVo userDate = userManageService.getUser(id);
		return R.ok().put("userDate", userDate);
	}
	
	@RequestMapping("getMangerInfo/{id}")
	@ResponseBody
	public R getMangerInfo(@PathVariable("id") int id) {
		MangerInfoVo mangerInfoVo = userManageService.getMangerInfo(id);
		return R.ok().put("mangerInfoVo", mangerInfoVo);
	}
	
	@RequestMapping("modifyUserPassword")
	@ResponseBody
	public R modifyUserPassword(MangerInfoVo mangerInfoVo) {
		mangerInfoVo.setPassword(SecretUtil.encrypt(mangerInfoVo.getPassword()));
		mangerInfoVo.setUpdateTime(new Date());
		int result = 0;
		result = userManageService.modifyUserPassword(mangerInfoVo);
		if (result == 1)
			return R.ok();
		else
			return R.error("系统故障！");
	}
}